﻿using APIShoppingSystem.GetSession;
using APIShoppingSystem.Model;
using IShoppingBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace APIShoppingSystem.Controllers
{
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class CommodityRecordController : ControllerBase
	{
        ICommodityRecordBll _commodityRecordBll;
        ICommodityInfoBll _commodityInfoBll;
        public CommodityRecordController(ICommodityRecordBll commodityRecordBll, ICommodityInfoBll commodityInfoBll)
        {
            _commodityRecordBll = commodityRecordBll;
            _commodityInfoBll = commodityInfoBll;
        }


        /// <summary>
        /// 添加商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCommodityRecord([FromForm] string CategoryId, [FromForm] int num)
        {
            CustomActionResult res = new CustomActionResult();


            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "未获取到登录用户的信息";
                return new JsonResult(res);
            }
            if (string.IsNullOrEmpty(CategoryId))
            {
                res.Msg = "商品不能为空";
                return new JsonResult(res);
            }
            else if (num == 0)
            {
                res.Msg = "数量不能为空";
                return new JsonResult(res);
            }
            Commodity_Record commodityRecord = new Commodity_Record()
            {
                ID = Guid.NewGuid().ToString(),
                GoodsId = CategoryId,
                Num = num,
                //Type = 1,
                CreateTime = DateTime.Now,
                Creator = userInfo.ID

            };

            bool isSuccess = _commodityRecordBll.AddCommodityRecord(commodityRecord);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return new JsonResult(res);
            }
            else
            {
                return new JsonResult(res);
            }
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="goodsId"></param>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetCommodityRecordList(int page, int limit, string goodsId)
        {
            int count;
            object commodityRecord = _commodityRecordBll.GetCommodityRecordListByPage(page, limit, out count, goodsId);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = commodityRecord
            });
        }


        /// <summary>
        /// 获取商品名字
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCommodityRecordSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            object options = _commodityInfoBll.GetCommodityRecordSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Datas = options;
            return new JsonResult(res);
        }


        //商品文件上传
        [HttpPost]
        public IActionResult Upload([FromForm] List<IFormFile> files)
        {
            CustomActionResult res = new CustomActionResult();
            var file = files.FirstOrDefault();
            //文件格式：NPOI 使用 HSSFWorkbook 类来处理 xls，XSSFWorkbook 类来处理 xlsx，
            //它们都继承接口 IWorkbook，因此可以通过 IWorkbook 来统一处理 xls 和 xlsx 格式的文件
            string[] geshi = file.FileName.Split(".");
            bool geshisss = false;
            if (geshi[1].Equals("xlsx"))
            {
                geshisss = true;

            }

            if (file != null)
            {
                var userInfoJson = HttpContext.Session.GetString("UserInfo");
                UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(userInfoJson);
                if (userInfo == null)
                {

                    return new JsonResult(res);
                }
                var stream = file.OpenReadStream();
                //获取上传excel文件，提交至数据库中
                string msg;
                bool isSuccess = _commodityRecordBll.Upload(stream, userInfo.ID, out msg, geshisss);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "上传成功";
                    return new JsonResult(res);
                }
                else
                {
                    res.Msg = msg;
                    return new JsonResult(res);
                }

            }
            return new JsonResult(res);
        }


        /// <summary>
        /// 文件下载
        /// </summary>
        /// <returns></returns>
       [HttpPost]
        public IActionResult Download()
        {
            //商品记录集合
            Stream stream = _commodityRecordBll.GetDownloadDatas();
            return File(stream, "application/octet-stream", "output.xlsx");
        }
    }
}
